<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="matchSearch" label-width="68px">
      <el-form-item label="日期" prop="date">
        <el-date-picker clearable
          v-model="queryParams.date"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['match:market:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['match:market:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['match:market:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['match:market:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :matchSearch.sync="matchSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="marketList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="股票编号" align="center" prop="gid" >
        <template slot-scope="scope">
          <span class="col-cont" v-html="showDate2(scope.row.gid)"></span>
        </template >
      </el-table-column>
      <el-table-column label="日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" >
        <template slot-scope="scope">
          <span class="col-cont" v-html="showDate(scope.row.name)"></span>
        </template >
      </el-table-column>
      <el-table-column label="买五" align="center" prop="buyFive" />
      <el-table-column label="买五的价格" align="center" prop="buyFivePri" />
      <el-table-column label="买四" align="center" prop="buyFour" />
      <el-table-column label="买四的价格" align="center" prop="buyFourPri" />
      <el-table-column label="买一" align="center" prop="buyOne" />
      <el-table-column label="买一的价格" align="center" prop="buyOnePri" />
      <el-table-column label="买三" align="center" prop="buyThree" />
      <el-table-column label="买三的价格" align="center" prop="buyThreePri" />
      <el-table-column label="买二" align="center" prop="buyTwo" />
      <el-table-column label="买二的价格" align="center" prop="buyTwoPri" />
      <el-table-column label="竞买价" align="center" prop="competitivePri" />
      <el-table-column label="涨跌百分比" align="center" prop="increPer" />
      <el-table-column label="涨跌额" align="center" prop="increase" />
      <el-table-column label="当前价格" align="center" prop="nowPri" />
      <el-table-column label="卖五" align="center" prop="sellFive" />
      <el-table-column label="卖五的价格" align="center" prop="sellFivePri" />
      <el-table-column label="卖四" align="center" prop="sellFour" />
      <el-table-column label="卖四的价格" align="center" prop="sellFourPri" />
      <el-table-column label="卖一" align="center" prop="sellOne" />
      <el-table-column label="卖一的价格" align="center" prop="sellOnePri" />
      <el-table-column label="卖三" align="center" prop="sellThree" />
      <el-table-column label="卖三的价格" align="center" prop="sellThreePri" />
      <el-table-column label="卖二" align="center" prop="sellTwo" />
      <el-table-column label="卖二的价格" align="center" prop="sellTwoPri" />
      <el-table-column label="时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="今日最高价格" align="center" prop="todayMax" />
      <el-table-column label="今日最低价格" align="center" prop="todayMin" />
      <el-table-column label="今日开始价格" align="center" prop="todayStartPri" />
      <el-table-column label="成交量(万)" align="center" prop="traAmount" />
      <el-table-column label="成交额" align="center" prop="traNumber" />
      <el-table-column label="昨日结束价格" align="center" prop="yestodEndPri" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['match:market:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['match:market:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="买五" prop="buyFive">
          <el-input v-model="form.buyFive" placeholder="请输入买五" />
        </el-form-item>
        <el-form-item label="买五的价格" prop="buyFivePri">
          <el-input v-model="form.buyFivePri" placeholder="请输入买五的价格" />
        </el-form-item>
        <el-form-item label="买四" prop="buyFour">
          <el-input v-model="form.buyFour" placeholder="请输入买四" />
        </el-form-item>
        <el-form-item label="买四的价格" prop="buyFourPri">
          <el-input v-model="form.buyFourPri" placeholder="请输入买四的价格" />
        </el-form-item>
        <el-form-item label="买一" prop="buyOne">
          <el-input v-model="form.buyOne" placeholder="请输入买一" />
        </el-form-item>
        <el-form-item label="买一的价格" prop="buyOnePri">
          <el-input v-model="form.buyOnePri" placeholder="请输入买一的价格" />
        </el-form-item>
        <el-form-item label="买三" prop="buyThree">
          <el-input v-model="form.buyThree" placeholder="请输入买三" />
        </el-form-item>
        <el-form-item label="买三的价格" prop="buyThreePri">
          <el-input v-model="form.buyThreePri" placeholder="请输入买三的价格" />
        </el-form-item>
        <el-form-item label="买二" prop="buyTwo">
          <el-input v-model="form.buyTwo" placeholder="请输入买二" />
        </el-form-item>
        <el-form-item label="买二的价格" prop="buyTwoPri">
          <el-input v-model="form.buyTwoPri" placeholder="请输入买二的价格" />
        </el-form-item>
        <el-form-item label="${comment}" prop="competitivePri">
          <el-input v-model="form.competitivePri" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="日期" prop="date">
          <el-date-picker clearable
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="gid">
          <el-input v-model="form.gid" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="increPer">
          <el-input v-model="form.increPer" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="increase">
          <el-input v-model="form.increase" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="当前价格" prop="nowPri">
          <el-input v-model="form.nowPri" placeholder="请输入当前价格" />
        </el-form-item>
        <el-form-item label="${comment}" prop="reservePri">
          <el-input v-model="form.reservePri" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="卖五" prop="sellFive">
          <el-input v-model="form.sellFive" placeholder="请输入卖五" />
        </el-form-item>
        <el-form-item label="卖五的价格" prop="sellFivePri">
          <el-input v-model="form.sellFivePri" placeholder="请输入卖五的价格" />
        </el-form-item>
        <el-form-item label="卖四" prop="sellFour">
          <el-input v-model="form.sellFour" placeholder="请输入卖四" />
        </el-form-item>
        <el-form-item label="卖四的价格" prop="sellFourPri">
          <el-input v-model="form.sellFourPri" placeholder="请输入卖四的价格" />
        </el-form-item>
        <el-form-item label="卖一" prop="sellOne">
          <el-input v-model="form.sellOne" placeholder="请输入卖一" />
        </el-form-item>
        <el-form-item label="卖一的价格" prop="sellOnePri">
          <el-input v-model="form.sellOnePri" placeholder="请输入卖一的价格" />
        </el-form-item>
        <el-form-item label="卖三" prop="sellThree">
          <el-input v-model="form.sellThree" placeholder="请输入卖三" />
        </el-form-item>
        <el-form-item label="卖三的价格" prop="sellThreePri">
          <el-input v-model="form.sellThreePri" placeholder="请输入卖三的价格" />
        </el-form-item>
        <el-form-item label="卖二" prop="sellTwo">
          <el-input v-model="form.sellTwo" placeholder="请输入卖二" />
        </el-form-item>
        <el-form-item label="卖二的价格" prop="sellTwoPri">
          <el-input v-model="form.sellTwoPri" placeholder="请输入卖二的价格" />
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="今日最高价格" prop="todayMax">
          <el-input v-model="form.todayMax" placeholder="请输入今日最高价格" />
        </el-form-item>
        <el-form-item label="今日最低价格" prop="todayMin">
          <el-input v-model="form.todayMin" placeholder="请输入今日最低价格" />
        </el-form-item>
        <el-form-item label="今日开始价格" prop="todayStartPri">
          <el-input v-model="form.todayStartPri" placeholder="请输入今日开始价格" />
        </el-form-item>
        <el-form-item label="${comment}" prop="traAmount">
          <el-input v-model="form.traAmount" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="traNumber">
          <el-input v-model="form.traNumber" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="昨日结束价格" prop="yestodEndPri">
          <el-input v-model="form.yestodEndPri" placeholder="请输入昨日结束价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMarket, getMarket, delMarket, addMarket, updateMarket } from "@/api/match/market";

export default {
  name: "Market",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      matchSearch: true,
      // 总条数
      total: 0,
      // 股票信息表格数据
      marketList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buyFive: null,
        buyFivePri: null,
        buyFour: null,
        buyFourPri: null,
        buyOne: null,
        buyOnePri: null,
        buyThree: null,
        buyThreePri: null,
        buyTwo: null,
        buyTwoPri: null,
        competitivePri: null,
        date: null,
        gid: null,
        increPer: null,
        increase: null,
        name: null,
        nowPri: null,
        reservePri: null,
        sellFive: null,
        sellFivePri: null,
        sellFour: null,
        sellFourPri: null,
        sellOne: null,
        sellOnePri: null,
        sellThree: null,
        sellThreePri: null,
        sellTwo: null,
        sellTwoPri: null,
        time: null,
        todayMax: null,
        todayMin: null,
        todayStartPri: null,
        traAmount: null,
        traNumber: null,
        yestodEndPri: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  computed:{
    /**
     * name 监视器
     */
    StockInfoList: function() {
      const handleQuery = this.queryParams.name;
      if (handleQuery) {
        return this.marketList.filter(data => {
          return Object.keys(data).some(key => {
            return String(data[key]).toLowerCase().indexOf(handleQuery) > -1
          })
        })
      }
      const handleQuery2 = this.queryParams.code;
      if (handleQuery2) {
        return this.marketList.filter(data => {
          return Object.keys(data).some(key => {
            return String(data[key]).toLowerCase().indexOf(handleQuery2) > -1
          })
        })
      }
      return this.marketList
    }
  },
  methods: {
    showDate(val) {
      val = val + '';
      if (val.indexOf(this.queryParams.name) !== -1 && this.queryParams.name !== '') {
        return val.replace( this.queryParams.name, '<font>' + this.queryParams.name + '</font>');
      } else {
        return val
      }
    },

    showDate2(val) {
      val = val + '';
      if (val.indexOf(this.queryParams.gid) !== -1 && this.queryParams.gid !== '') {
        return val.replace( this.queryParams.gid, '<font>' + this.queryParams.gid + '</font>');
      } else {
        return val
      }
    },
    /** 查询股票信息列表 */
    getList() {
      this.loading = true;
      listMarket(this.queryParams).then(response => {
        console.log('111')
        console.log(response)
        this.marketList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        buyFive: null,
        buyFivePri: null,
        buyFour: null,
        buyFourPri: null,
        buyOne: null,
        buyOnePri: null,
        buyThree: null,
        buyThreePri: null,
        buyTwo: null,
        buyTwoPri: null,
        competitivePri: null,
        date: null,
        gid: null,
        increPer: null,
        increase: null,
        name: null,
        nowPri: null,
        reservePri: null,
        sellFive: null,
        sellFivePri: null,
        sellFour: null,
        sellFourPri: null,
        sellOne: null,
        sellOnePri: null,
        sellThree: null,
        sellThreePri: null,
        sellTwo: null,
        sellTwoPri: null,
        time: null,
        todayMax: null,
        todayMin: null,
        todayStartPri: null,
        traAmount: null,
        traNumber: null,
        yestodEndPri: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.buyFive)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加股票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const buyFive = row.buyFive || this.ids
      getMarket(buyFive).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改股票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buyFive != null) {
            updateMarket(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMarket(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buyFives = row.buyFive || this.ids;
      this.$modal.confirm('是否确认删除股票信息编号为"' + buyFives + '"的数据项？').then(function() {
        return delMarket(buyFives);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('match/market/export', {
        ...this.queryParams
      }, `market_${new Date().getTime()}.pdf`)
    }
  }
};
</script>
